﻿using CthulhuDomain.GameEntities;
using CthulhuDomain.Services;
using CthulhuDomain.StageManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CthulhuDomain;
using System.Linq;

namespace Invaders
{
  public class CthulhuInvaders : Game
  {
    #region Props

    protected DomainEntityMatrix Army { get; set; }
    protected DomainEntity Player { get; set; }
    protected GraphicsDeviceManager Graphics { get; set; }
    protected IContentManager ManagerAdapter { get; set; }
    protected ISpriteBatch SpriteBatch { get; set; }

    protected Rectangle ClientBounds
    {
      get { return Window.ClientBounds; }
    }

    protected IRectangle Context { get; set; }

    #endregion

    #region Ctor

    public CthulhuInvaders()
    {
      Graphics = new GraphicsDeviceManager(this);
      Content.RootDirectory = "Content";
      Graphics.IsFullScreen = false;
      Graphics.IsFullScreen = true;
    }

    #endregion

    #region Methods

    protected override void Initialize()
    {
      SpriteBatch = new SpriteBatchAdapter(GraphicsDevice);
      ManagerAdapter = new ContentManagerAdapter(Content);
      Context = new RectangleAdapter(ClientBounds.X, ClientBounds.Y, ClientBounds.Width, ClientBounds.Height);

      var serviceManager = new GameServiceManager(new GameAdapter(this),
                                                  ManagerAdapter,
                                                  SpriteBatch,
                                                  Context);

      StateManager.Initialize(serviceManager);

      base.Initialize();
    }

    protected override void LoadContent()
    {
      StateManager.LoadContent();
    }

    protected override void Update(GameTime gameTime)
    {
      if(GetPressedKeys().Contains(Keys.Escape)) Exit();

      StateManager.Update(GetPressedKeys(), new GameTimeAdapter(gameTime));

      base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
      StateManager.Draw();

      base.Draw(gameTime);
    }


    private static Keys[] GetPressedKeys()
    {
      return Keyboard.GetState(PlayerIndex.One).GetPressedKeys();
    }

    #endregion

  }


}
